<?php

class Admin_resellers_model extends CI_Model{

	//count total rows
	function listResellers(){
		$qstr = $this->_buildQstr();
		$query = $this->db->query($qstr);
		return $query->num_rows;
	}
	//to be used in pagination
	function listResellers_limited( $start_row=0, $limit=100 ){
		$qstr = $this->_buildQstr();
		$qstr .= " LIMIT $start_row, $limit";
		$query = $this->db->query($qstr);
		return ($query->num_rows() > 0) ? $query->result() : false;
	}
	//returns query string
	function _buildQstr(){
		$filter = @$_SESSION['search_text'];
		$key = $this->Quote('%'.mysql_real_escape_string( trim( strtolower( $filter ) ) ).'%');
		if($filter){		//if search is not empty
			$qstr = "SELECT *
						FROM (
						`mlme_users` tbl
						)
						WHERE (LOWER( tbl.email ) LIKE $key
						OR LOWER( tbl.firstname ) LIKE $key
						OR LOWER( tbl.lastname ) LIKE $key
						OR LOWER( tbl.middle ) LIKE $key)
						AND (type='reseller')";
		} else {
			$qstr = "SELECT *
						FROM (
						`mlme_users` tbl
						)
						WHERE tbl.type='reseller'";
		}
		return $qstr;
	}
	function Quote( $text, $escaped = true ){
		return '\''.($escaped ? mysql_real_escape_string( $text ) : $text).'\'';
	}
/*	function list_resellers($start_row='', $limit=''){
		$limit_txt = (!empty($start_row) && !empty($limit))? 'LIMIT '.$start_row.','.$limit:'';
		$query = $this->db->query("SELECT * FROM mlme_users WHERE type='reseller' $limit_txt");
		return $query->result_array();
	}//end fct
	function search($keyword_data){
		$keyword = $keyword_data;
		if(isset($keyword_data)&&(!empty($keyword_data))){
			$where_in = '';
			$replace = str_replace(" ","','",$keyword_data);
			if(isset($keyword) && !empty($keyword)) $where_in .= "firstname IN('$replace') OR";
			if(isset($keyword) && !empty($keyword)) $where_in .= " lastname IN('$replace') OR";
			if(isset($keyword) && !empty($keyword)) $where_in .= " middle IN('$replace') OR";
			if(isset($keyword) && !empty($keyword)) $where_in .= " email IN('$replace')";
		}
		$query = $this->db->query("SELECT * FROM mlme_users WHERE type='reseller' AND ($where_in)");
		return ($query->num_rows() > 0) ? $query->result_array() : FALSE ;
	}*/
	//get the accounts data of the reseller
	//@param int: id of the user
	//return array of object
	function getResellerAccounts($userid){
		$qstr = "SELECT a.code, a.status, b.customerid, c.firstname, c.middle, c.lastname, c.date_added, c.email" .
				" FROM mlme_usersproduct a, mlme_usersproduct_customers b, mlme_users c" .
				" WHERE a.userid=$userid" .
				" AND a.userproductid=b.userproductid" .
				" AND b.customerid=c.userid";
		return $this->db->query($qstr)->result();
	}
	function getResellerStats($userid){
	 	$this->db->select('userid');
	 	$this->db->where('type','reseller');
	 	$qry = $this->db->get('mlme_users');
		if($qry->num_rows < 0){
			return false;
		}
		$stats = null;
	 	$this->db->select('COUNT(userproductid) as numPackages');
	 	$this->db->where('userid', $userid);
	 	$qry = $this->db->get('mlme_usersproduct')->result();
	 	$stats->numPackages = $qry[0]->numPackages;

	 	$this->db->select('SUM(accounts_qty) as numAccounts');
	 	$this->db->where('userid', $userid);
	 	$this->db->where('status', 1);
	 	$qry = $this->db->get('mlme_usersproduct')->result();
	 	$stats->numAccounts = $qry[0]->numAccounts;

		$qstr = "SELECT COUNT(a.customerid) as numRegistered FROM mlme_usersproduct_customers a, mlme_usersproduct b" .
				" WHERE a.userproductid=b.userproductid " .
				" AND b.userid = $userid";
	 	$qry = $this->db->query($qstr);
	 	$result = $qry->result();
	 	$stats->numRegistered = $result[0]->numRegistered;

		$stats->numRemaining = (int)$stats->numAccounts - (int)$stats->numRegistered;

		return $stats;
	}
	function update_user_status($userid,$status){
		$data = array('status'=>$status,'date_updated'=>date('Y-m-d H:i:s'));
		$this->db->where('userid',$userid);
		return $this->db->update('mlme_users',$data);
	}//end fct
	//delete data
	function deleteUser( $id=array() ){
		if( empty($id) ) return false;
		$this->db->where_in( 'userid', $id );
		return $this->db->delete('mlme_users');
	}

}